// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "board.h"
#include "esp_log.h"

#include "i2c_bus.h"
#include "spi_bus.h"




static const char *TAG = "board_verison";

#ifdef CONFIG_BOARD_ESP32_HARPER_V1_1
static const char *board_name = "ESP32-Harper-V1.1";
#elif  CONFIG_BOARD_ESP32_HARPER_V1_0
static const char *board_name = "ESP32-Harper-V1.0";
#else 
#error "No board is defined"
#endif

static bool board_is_init = false;
static spi_bus_handle_t board_spi2_bus_handle = NULL;



/* static func */

static esp_err_t board_spi_bus_init(void)
{

    spi_config_t bus_conf = {
        .sclk_io_num = BOARD_IO_SPI2_SCK,
        .miso_io_num = BOARD_IO_SPI2_MISO,
        .mosi_io_num = BOARD_IO_SPI2_MOSI,
        .max_transfer_sz = 2*240*135 + 10,
    };

    board_spi2_bus_handle = spi_bus_create(SPI2_HOST, &bus_conf);
    if (board_spi2_bus_handle == NULL) {
        ESP_LOGE(TAG, "spi2 create failed!\n");
        return ESP_FAIL;
    }

    return ESP_OK;
}

static esp_err_t board_spi_bus_deinit(void)
{
    if (board_spi2_bus_handle != NULL) {
        spi_bus_delete(&board_spi2_bus_handle);
        if (board_spi2_bus_handle != NULL) {
            ESP_LOGE(TAG, "spi2 delete failed!\n");
            return ESP_FAIL;
        }
    }
    return ESP_OK;
}


/* export func */

esp_err_t harper_board_init(void)
{
    int ret;

    if (board_is_init != false) {
        return ESP_OK;
    }

    /* init i2c bus */
    


    /* init spi bus */
    ret = board_spi_bus_init();
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "spi init failed!\n");
        return ret;
    }

    board_is_init = true;

    ESP_LOGI(TAG, "[ %s ]\n", board_name);

    return ESP_OK;
}

esp_err_t harper_board_deinit(void)
{
    int ret;
    if (board_is_init == false) {
        return ESP_OK;
    }


    

    /* deinit spi bus */
    ret = board_spi_bus_deinit();
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "spi deinit failed!\n");
        return ret;
    }

    board_is_init = false;
    ESP_LOGI(TAG, "[ %s deinit ]\n", board_name);

    return ESP_OK;
}

char *harper_board_get_name(void)
{
    return (char*)board_name;
}

board_res_handle_t harper_board_get_handle(board_res_id_t id)
{
    board_res_handle_t handle = NULL;

    switch (id)
    {
    case BOARD_I2C0_ID:
        
        break;
    case BOARD_SPI2_ID:
        handle = board_spi2_bus_handle;
        break;
    
    default:
        handle = NULL;
        break;
    }

    return handle;
}


